package hip.util.lang;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringType {

    public static boolean isDate(String value, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            sdf.setLenient(false);
            sdf.parse(value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isBase64(String value, String charset) {
		 /*if(value!=null){
			 try {
				 String strDecode = Base64Utils.base64UnEscape(value, charset);
				 String strEncode = Base64Utils.base64Escape(strDecode, charset);
		        if(strEncode.equals(value)){
		        	return true;
		        }
		    } catch (UnsupportedEncodingException e) {
		        return false;
		    }   
		 }
		 return false;*/
        return true;
    }

    public static boolean isNumber(String value) {
        if (value != null) {
            String regEx = "^(-?\\d+)(\\.\\d+)?$";
            Pattern p = Pattern.compile(regEx);
            if (p.matcher(value).find()) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasNumber(String value) {
        if (value != null) {
            String regEx = "[0-9]+?";
            Pattern p = Pattern.compile(regEx);
            if (p.matcher(value).find()) {
                return true;
            }
        }
        return false;
    }

    public static List<String> getPattern(String value, String regEx) {
        if (value != null) {
            List<String> rs = new ArrayList<>();
            Pattern isN = Pattern.compile(regEx);
            Matcher m = isN.matcher(value);
            while (m.find()) {
                rs.add(m.group());
            }
            return rs.size() > 0 ? rs : null;
        }
        return null;
    }

    public static String handlerEscapeChar(String str) {
        //这边线注释掉，不再替换2018/7/17
//		 if(str!=null){
//			str = str.replaceAll("<", "小于");//<
//			str = str.replaceAll("&", "和");//&
//		 }
        return str;
    }

    public static void main(String[] args) {
        //System.out.println(isNumber("1.00"));
        //System.out.println(handlerEscapeChar("&lt;hhkhk"));
    }
}
